<?php

namespace App\Http\Controllers;

use App\Http\Model\Hint;
use App\Http\Model\OutJson;
use App\Http\Services\ComeBackServices;
use App\Http\Services\UserServices;
use App\Lib\Response;
use Illuminate\Http\Request;


class ComeBackController extends BaseController
{
    protected $userService;
    protected $comeBackService;

    public function __construct()
    {
        $this->userService = new UserServices();
        $this->comeBackService = new ComeBackServices();
    }

    //获取悬浮图权限
    public function getFloatIcon(Request $request){
        $user = $this->userService->get_current_user();
        if (empty($user)) {
            Response::returnResult(Hint::LOGIN_OUT);
        }
        $user_id = $user['user_id'];
        //查看用户是否在回归用户名单内
        $res = $this->comeBackService->getFloatIcon($user_id);
        Response::returnResult(Hint::SUCCESS,$res['data'],$res['msg']);
    }

    //进行签到
    public function doSigin(Request $request){
        $siginId = $request->post('sign_id',0);
        $actId = $request->post('act_id',0);
        if(empty($siginId)){
            Response::returnResult(Hint::PARAM_ERROR);
        }
        if(empty($actId)){
            Response::returnResult(Hint::PARAM_ERROR);
        }
        $user = $this->userService->get_current_user();
        if (empty($user)) {
            Response::returnResult(Hint::LOGIN_OUT);
        }
        $user_id = $user['user_id'];
        $res = $this->comeBackService->doSigin($user_id, $siginId, $actId);
        if($res['code'] != Hint::SUCCESS){
            Response::returnResult(Hint::ERR_NORMAL,$res['data'],$res['msg']);
        }
        Response::returnResult(Hint::SUCCESS,$res['data'],$res['msg']);
    }

    //获取每日任务列表
    public function getMissionList(Request $request){
        $actId = $request->get('act_id',0);
        if(empty($actId)){
            Response::returnResult(Hint::PARAM_ERROR);
        }
        $user = $this->userService->get_current_user();
        if (empty($user)) {
            Response::returnResult(Hint::LOGIN_OUT);
        }
        $user_id = $user['user_id'];
        $res = $this->comeBackService->getMissionList($user_id, $actId);
        if($res['code'] != Hint::SUCCESS){
            Response::returnResult(Hint::ERR_NORMAL,$res['data'],$res['msg']);
        }
        Response::returnResult(Hint::SUCCESS,$res['data'],$res['msg']);
    }

    //领取任务奖励
    public function getAward(Request $request){
        $taskId = $request->post('task_id',0);
        if(empty($taskId)){
            Response::returnResult(Hint::PARAM_ERROR);
        }
        $user = $this->userService->get_current_user();
        if (empty($user)) {
            Response::returnResult(Hint::LOGIN_OUT);
        }
        $user_id = $user['user_id'];
        $res = $this->comeBackService->getAward($user_id, $taskId);
        if($res['code'] != Hint::SUCCESS){
            Response::returnResult(Hint::ERR_NORMAL,$res['data'],$res['msg']);
        }
        Response::returnResult(Hint::SUCCESS,$res['data'],$res['msg']);
    }
}